home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / Multi-Panel Dialogs 1.1 / MPD Sources / Generic Classes / CTargeterBorder.cp < prev    next >
Encoding:
Text File  |  1996-09-09  |  7.1 KB  |  222 lines  |  [TEXT/R*ch]

  1. // ===========================================================================
  2. //    File:                        CTargeterBorder.cp
  3. // Version:                    1.0 - Feb 1, 1996
  4. //    Author:                    Mike Shields (mshields@inconnect.com)
  5. //                            
  6. //    Copyright ©1996 Mike Shields. All rights reserved.
  7. //    I hereby grant users of CTargeterBorder permission to use it (or any modified 
  8. //    version of it) in applications (or any other type of Macintosh software 
  9. //    like extensions -- freeware, shareware, commercial, or other) for free, 
  10. //    subject to the terms that:
  11. //
  12. //        (1)  This agreement is non-exclusive.
  13. //
  14. //        (2)  I, Mike Shields, retain the copyright to the original source code.
  15. //
  16. //    These two items are the only required conditions for use. However, I do have 
  17. //    an additional request. Note, however, that this is only a request, and 
  18. //    that it is not a required condition for use of this code.
  19. //
  20. //        (1) That I be given credit for CTargeterBorder code in the copyrights or 
  21. //            acknowledgements section of your manual or other appropriate documentation.
  22. //
  23. //
  24. //    I would like to repeat that this last item is only a request. You are prefectly 
  25. //    free to choose not to do any or all of them.
  26. //    
  27. //        This source code is distributed in the hope that it will be useful,
  28. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. // ===========================================================================
  31. //    CTargeterBorder.h        <- double-click + Command-D to see class declaration
  32. //
  33. // Class providing a "focus box" around a pane
  34.  
  35. #include    "CTargeterBorder.h"
  36.  
  37. #include    <UDrawingState.h>
  38. #include    <LStream.h>
  39. #include    <URegions.h>
  40.  
  41. #include "CTargetable.h"
  42.  
  43. #pragma mark === Construction & Destruction ===
  44.  
  45. //----------------------------------------------------------------------------------------
  46. // CTargeterBorder::CreateFromStream
  47. //----------------------------------------------------------------------------------------
  48. // Static function registered with URegistrar to create a TargeterBorder from the data 
  49. // in a stream
  50. CTargeterBorder* CTargeterBorder::CreateFromStream(LStream* inStream)
  51. {
  52.     return (new CTargeterBorder(inStream));
  53. }
  54.  
  55. //----------------------------------------------------------------------------------------
  56. // CTargeterBorder::CTargeterBorder
  57. //----------------------------------------------------------------------------------------
  58. // Default Contructor
  59. CTargeterBorder::CTargeterBorder()
  60. {
  61.     mTarget = nil;
  62.     mIsTargeted = false;
  63. }
  64.  
  65. //----------------------------------------------------------------------------------------
  66. // CTargeterBorder::CTargeterBorder
  67. //----------------------------------------------------------------------------------------
  68. // Construct from the data in a Stream
  69. CTargeterBorder::CTargeterBorder(LStream* inStream)
  70.     : LView(inStream)
  71. {
  72.     mTarget = nil;
  73.     mIsTargeted = false;
  74.     
  75.     inStream->ReadData(&mTargetedPaneID, sizeof(PaneIDT));
  76. }
  77.  
  78. //----------------------------------------------------------------------------------------
  79. // CTargeterBorder::~CTargeterBorder
  80. //----------------------------------------------------------------------------------------
  81. // Destructor
  82. CTargeterBorder::~CTargeterBorder()
  83. {
  84. }
  85.  
  86. //----------------------------------------------------------------------------------------
  87. // CTargeterBorder::FinishCreateSelf
  88. //----------------------------------------------------------------------------------------
  89. //    Set up our view so that we can be notified when the targeted pane becomes the target.
  90. void CTargeterBorder::FinishCreateSelf(void)
  91. {
  92.     mTarget = dynamic_cast<CTargetable*>(this->FindPaneByID(mTargetedPaneID));
  93.     SignalIf_(mTarget == nil);
  94.     
  95.     mTarget->SetTargeterBox(this);
  96.     if ( mTarget->IsTargeted() )
  97.         this->ShowFocus();
  98.     else
  99.         this->HideFocus();
  100. }
  101.  
  102. #pragma mark === Drawing ===
  103. //----------------------------------------------------------------------------------------
  104. // CTargeterBorder::DrawSelf
  105. //----------------------------------------------------------------------------------------
  106. //    Draw the targeter box outline
  107. void CTargeterBorder::DrawSelf()
  108. {
  109.     Rect    frame;
  110.     if ( mIsTargeted && CalcLocalFrameRect(frame) ) 
  111.     {
  112.         ApplyForeAndBackColors();
  113.         ::PenNormal();
  114.         ::PenSize(2, 2);
  115.         ::FrameRect(&frame);
  116.     }
  117. }
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // CTargeterBorder::Refresh
  121. //----------------------------------------------------------------------------------------
  122. //    Invalidate the area occupied by a TargeterBorder so it will redraw during
  123. //    the next Update event
  124. void CTargeterBorder::Refresh()
  125. {
  126.     Rect    frame;
  127.     if ( IsVisible() &&
  128.          CalcPortFrameRect(frame) &&
  129.          (mSuperView != nil) ) 
  130.     {
  131.         Rect    revealed;
  132.         mSuperView->GetRevealedRect(revealed);
  133.         if (::SectRect(&frame, &revealed, &revealed)) 
  134.         {
  135.             RgnHandle    boxRgn = GetBoxRegion(frame, revealed);
  136.             InvalPortRgn(boxRgn);
  137.             ::DisposeRgn(boxRgn);
  138.         }
  139.     }
  140. }
  141.  
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // CTargeterBorder::DontRefresh
  145. //----------------------------------------------------------------------------------------
  146. //    Validate the area occupied by a TargeterBorder so it won't redraw during
  147. //    the next Update event
  148. void CTargeterBorder::DontRefresh(Boolean inOKIfHidden)
  149. {
  150.     Rect    frame;
  151.     if ( (IsVisible() || inOKIfHidden) &&
  152.          CalcPortFrameRect(frame) &&
  153.          (mSuperView != nil) ) 
  154.     {
  155.         Rect    revealed;
  156.         mSuperView->GetRevealedRect(revealed);
  157.         if (::SectRect(&frame, &revealed, &revealed)) 
  158.         {
  159.             RgnHandle    boxRgn = GetBoxRegion(frame, revealed);
  160.             ValidPortRgn(boxRgn);
  161.             ::DisposeRgn(boxRgn);
  162.         }
  163.     }
  164. }
  165.  
  166.  
  167. // ---------------------------------------------------------------------------
  168. //        • GetBoxRegion
  169. // ---------------------------------------------------------------------------
  170. //    Pass back a region defining a TargeterBorder. A TargeterBorder is a hollow
  171. //    rectangular region. Clip region so that it is within the revealed rect.
  172. //
  173. //    Creates a new region which the caller must dispose.
  174. RgnHandle CTargeterBorder::GetBoxRegion(const Rect &inFrame, const Rect &inRevealed) const
  175. {
  176.     RgnHandle    boxRgn = ::NewRgn();
  177.     ::OpenRgn();
  178.     Rect    r = inFrame;
  179.     ::FrameRect(&r);                    // Outer edge of box
  180.     ::InsetRect(&r, 2, 2);
  181.     ::FrameRect(&r);                    // Inner edge of box
  182.     ::CloseRgn(boxRgn);
  183.     
  184.                                         // Clip to revealed rect
  185.     RgnHandle    revealedRgn = ::NewRgn();
  186.     ::RectRgn(revealedRgn, &inRevealed);
  187.     ::SectRgn(boxRgn, revealedRgn, boxRgn);
  188.     ::DisposeRgn(revealedRgn);
  189.     
  190.     return boxRgn;
  191. }
  192.  
  193. #pragma mark === Focus Management ===
  194. //----------------------------------------------------------------------------------------
  195. // CTargeterBorder::ShowFocus
  196. //----------------------------------------------------------------------------------------
  197. //    A TargeterBorder is being shown
  198. void CTargeterBorder::ShowFocus()
  199. {
  200.     if ( !mIsTargeted ) 
  201.     {
  202.         mIsTargeted = true;
  203.         Refresh();
  204.     }
  205. }
  206.  
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // CTargeterBorder::HideFocus
  210. //----------------------------------------------------------------------------------------
  211. //    A TargeterBorder is being hidden
  212. void CTargeterBorder::HideFocus()
  213. {
  214.     if ( mIsTargeted ) 
  215.     {
  216.         mIsTargeted = false;
  217.         Refresh();
  218.     }
  219. }
  220.  
  221.  
  222.